package com.facotry.controller;

import com.alibaba.excel.EasyExcel;
import com.facotry.entity.worker;
import com.facotry.service.workerService;
import com.facotry.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * --- 代码敲烂 月薪过万 ---
 *
 * @author 崔瑜航
 * @date 2024/7/24
 * @desc
 */
@RestController
@RequestMapping("/worker")
@CrossOrigin
public class workerController {


    /**
     * 查询所有员工
     */
    @Autowired
    private workerService workerService;
    @GetMapping("/list")
    public R findAllWorker(@RequestParam HashMap<String,String> map){
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);
        List<worker> list = workerService.findAllWorker(map);
        PageInfo<worker> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    /**
     * 添加员工信息
     */
    @GetMapping("/add")
    private R addworker(worker worker){
        boolean isadd = workerService.addworker(worker);
        return R.ok(isadd);
    }
    /**
     * 更新员工信息
     */
    @PostMapping  ("/updateworker")
    public R updateworker(@RequestBody worker worker){
        // System.out.println("worker = " + worker);
        boolean isedit = workerService.updateworker(worker);
        return R.ok(isedit);
    }
    /**
     * 单个删除
     */
    @GetMapping("/del")
    public R del(int wId){
        boolean isdel = workerService.del(wId);
        return R.ok(isdel);
    }
    /**
     * 批量删除
     */
    @RequestMapping("/delBatch")
    public R delBatch(@RequestParam List<String> ids){
        boolean isdelBatch = workerService.delBatch(ids);
        return R.ok();
    }
    /**
     * 获得柱形图的数据
     */
    @GetMapping("/getNum")
    public R getNum(){
        return workerService.getNum();
    }
    /**
     * 上传Excel文件
     */
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ),
                            worker.class,
                            new com.facotry.entity.workerReadListener(workerService))
                    .sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok( );
    }

    /**
     * 下载Excel文件
     * 注意,返回值是void
     */
    @GetMapping("/download/excel")
    public void downloadExcel(HttpServletResponse response) throws IOException {

        //  查询数据库全部数据
        List<worker> list = workerService.findAllWorker(null);

        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("员工信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), worker.class).autoCloseStream(Boolean.FALSE).sheet("员工信息")
                .doWrite(list);
    }

}
